package fr.mywiki.model.jdbcAdapter.utils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import fr.mywiki.model.jdbcAdapter.values.ValueObjectFacade;
import fr.mywiki.model.queries.DatabaseAdapter;


/**
 * This manager is able to determine which keys are being currently used for a specified type and
 * which one are available.
 * 
 * @author tlombard
 */
public class KeyManager {
	/**
	 * Returns the set of keys that have already been used for this data type.
	 * 
	 * @param type a String representing a data type.
	 * @return a Collection of <code>Object</code> whose type depends on the type of the
	 *         identifier of the data type and whose value is the one of the identifier of the
	 *         record, null if the type is not correct.
	 */
	public Collection getUsedKeys(String type) throws SQLException {
		String identifierField = ValueObjectFacade.getIdentifierFieldName(type);
		String sql = "SELECT " + identifierField + " FROM " + type;
		
		Iterator itRecords = DatabaseAdapter.executeSelectQuery(sql).iterator();
		Collection keys = new ArrayList();

		while (itRecords.hasNext())
			keys.add(((Object[])itRecords.next())[0]);

		return keys;
	}
	
	/**
	 * Computes and returns a valid and unused key for the given type.
	 * 
	 * @param type a String representing 
	 * @return
	 */
	public Object getNewKey(String type) {
		return null;
	}
}
